<?php

class ectools_mdl_archive_payments extends archive_model {

    var $has_many = array(
        'orders' => 'archive_order_bills:contrast:payment_id^bill_id'
    );

    var $defaultOrder = array(
        't_payed',
        'DESC'
    );

    public function extra_search_info() {
        return array(
            'key' => array(
                'column' => 'payment_id',
                'label' => '收款单号'
            ),
            'time_column' => 't_begin'
        );
    }

    public function document2time($payment_id) {
        return array(
            'start' => strtotime(date('Ymd H:00:00', substr($payment_id, 0, 10))),
            'end' => strtotime(date('Ymd H:59:59', substr($payment_id, 0, 10)))
        );
    }

    public function _filter($filter, $tableAlias = null, $baseWhere = null) {
        if (! $filter)
            return parent::_filter($filter);
        
        if (array_key_exists('rel_id', $filter)) {
            $obj_order_bills = $this->app->model('archive_order_bills');
            $bill_filter = array(
                'rel_id|has' => $filter['rel_id'],
                'bill_type' => 'payments'
            );
            $row_order_bills = $obj_order_bills->getList('bill_id', $bill_filter);
            $arr_member_id = array();
            if ($row_order_bills) {
                $arr_order_bills = array();
                foreach ($row_order_bills as $arr) {
                    $arr_order_bills[] = $arr['bill_id'];
                }
                $filter['payment_id|in'] = $arr_order_bills;
            } else {
                $filter['payment_id'] = 'a';
            }
            unset($filter['rel_id']);
        }
        
        $filter = parent::_filter($filter);
        return $filter;
    }

    public function modifier_member_id($row) {
        if (is_null($row) || empty($row)) {
            return app::get('ectools')->_('未知会员或非会员');
        }
        
        return app::get('ectools')->_('未知会员或非会员');
    }

    public function modifier_op_id($row) {
        if (is_null($row) || empty($row)) {
            return app::get('ectools')->_('未知操作员');
        }
        
        $obj_pam_account = app::get('pam')->model('account');
        $arr_pam_account = $obj_pam_account->getList('login_name', array(
            'account_id' => $row
        ));
        return $arr_pam_account[0]['login_name'] ? $arr_pam_account[0]['login_name'] : app::get('ectools')->_('未知操作员');
    }

    public function modifier_pay_app_id($row) {
        $obj_payment_cfgs = $this->app->model('payment_cfgs');
        $arr_payment_cfgs = $obj_payment_cfgs->getPaymentInfo($row);
        
        if ($arr_payment_cfgs) {
            return $arr_payment_cfgs['app_name'];
        } else
            return 'app_name';
    }

    public function modifier_cur_money($row) {
        $currency = $this->app->model('currency');
        $filter = array(
            'payment_id' => $this->pkvalue
        );
        $tmp = $this->getList('currency', $filter);
        $arr_cur = $currency->getcur($tmp[0]['currency']);
        $row = $currency->formatNumber($row, false, false);
        
        return $arr_cur['cur_sign'] . $row;
    }

    public function modifier_ip($row) {
        if (is_null($row) || empty($row)) {
            return '-';
        }
        
        return $row;
    }
}
